const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istype: 1,
    nyinit: 0,
    disabled: true,
    date: '',
    dateNo: "",
    responsible: '',
    // responsibleOptions: [],
    responsibleList: [],
    dataList: [],
    userInfo: null,
    orderData: null,
    gradeList: null,
    queryFrom: {
      // "hospitalDepartId": 0,
      // "hospitalId": 0,
      // "year": "",
      // "yearAndMonth": ""
    },
    territoryList: [],
    salesPersonList: [{
      value: '0',
      label: '全部',
      checked: false
    }, ],
    rate:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: app.globalData.userInfo,
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
    // this.getTyoeList()
    // this.getPT()
    // this.getPE()
    // this.getList()
    this.getListzrr()
    this.getdisList()
  },
  getgrade() {
    let that = this
    if (that.data.orderData && that.data.orderData.isapp) {
      let newData = {}
      if (that.data.nyinit == 1) {
        newData.year = that.data.dateNo
      } else {
        newData.yearAndMonth = that.data.dateNo
      }
      newData.province = that.data.orderData.province?that.data.orderData.province:''
      newData.salesPerson = that.data.orderData.salesPerson?that.data.orderData.salesPerson:''
      wx.request({
        url: app.globalData.serverUrlgdb + '/statistics/count/grade',
        method: 'POST',
        header: {
          'token': wx.getStorageSync('token')
        },
        data: newData,
        success:res=>{
          let resData = res.data
        if (resData.code == 200) {
          console.log(resData.data.gradeList,'数据');
          that.setData({
            gradeList: resData.data.gradeList
          })
        }
        }
      })
    }
  },
  getgraderate(){
    let that = this
    if (that.data.orderData && that.data.orderData.isapp) {
      let newData = {}
      if (that.data.nyinit == 1) {
        newData.year = that.data.dateNo
      } else {
        newData.yearAndMonth = that.data.dateNo
      }
      newData.province = that.data.orderData.province?that.data.orderData.province:''
      newData.salesPerson = that.data.orderData.salesPerson?that.data.orderData.salesPerson:''
      wx.request({
        url: app.globalData.serverUrlgdb + '/statistics/count/grade/rate',
        method: 'POST',
        header: {
          'token': wx.getStorageSync('token')
        },
        data: newData,
        success:res=>{
          let resData = res.data
        if (resData.code == 200) {
          console.log(resData.data,'45645456456');
          that.setData({
            rate: resData.data
          })
        }
        }
      })
    }
  },
  
  
  getListzrr() {
    let that = this
    that.setData({
      salesPersonList:[]
    })
    wx.request({
      url: (wx.getStorageSync('tabNav') == 3 ? app.globalData.serverUrlgdb : app.globalData.serverUrl) + '/applet/get/person',
      method: 'get',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        let salesPersonList = []
        if (resp.code == 200) {
          if (resp.data.salesPersonList.length != 0) {
            resp.data.salesPersonList.map(item => {
              let obj = {}
              obj.label = item
              obj.value = item
              obj.checked = false
              salesPersonList.push(obj)
            })
            salesPersonList.unshift({
              value: '',
              label: '全部',
              checked: true
            })
            that.setData({
              salesPersonList
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log(app.globalData.userInfo);
  },
  getapplist() {
    let that = this
    wx.request({
      url: app.globalData.serverUrlgdb + '/st/detail/' + app.globalData.userInfo.staffId,
      method: 'get',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        if (resp.code == 200) {
          const orderData = that.data.orderData
          if (resp.data.staff.appletIdList.length == 0) {
            orderData.isapp = false
          } else {
            orderData.isapp = true
          }
          that.setData({
            orderData
          })
          this.getgrade()
          this.getgraderate()
        }
      }
    })
  },
  getTyoeList() {
    let that = this
    wx.request({
      url: app.globalData.serverUrlgdb + '/st/sort/' + that.data.istype,
      method: 'get',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        if (resp.code == 200) {
          resp.data.hospitalList.map(item => {
            item.value = item.hospitalId
            item.label = item.hospitalName
            // if(item.hospitalId==that.data.userInfo.hospitalId){
            //   item.checked = true
            // }
          })
          that.setData({
            responsibleList: resp.data.hospitalList,
          })
          that.getList()
        }
      }
    })
  },
  getdisList() {
    let that = this
    that.setData({
      territoryList:[]
    })
    wx.request({
      url: app.globalData.serverUrlgdb + '/district/province/relate/list',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        if (resp.code == 200) {
          resp.data.provinceList.map(item => {
            item.value = item.id
            item.label = item.name
            item.checked = false
          })
          resp.data.provinceList.unshift({
            value: '0',
            label: '全部',
            checked: true
          })
          that.setData({
            territoryList: resp.data.provinceList
          })
        }
      }
    })
  },
  getList() {
    let that = this
    let newData = {}
    wx.showLoading({
      title: '',
      mask: true
    })
    newData.type = that.data.istype
    if (that.data.nyinit == 1) {
      newData.year = that.data.dateNo
    } else {
      newData.yearAndMonth = that.data.dateNo
    }
    if (that.data.queryFrom.hospitalId) {
      newData.hospitalId = that.data.queryFrom.hospitalId
    }
    if (that.data.queryFrom.hospitalDepartId) {
      newData.hospitalDepartId = that.data.queryFrom.hospitalDepartId
    }
    wx.request({
      url: app.globalData.serverUrlgdb + '/statistics/count/by',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          console.log(resData.data);
          // resData.data.dataStatistics.istab = true
          resData.data.deptName = that.data.queryFrom.deptName
          that.setData({
            orderData: resData.data
          })
          this.getapplist()
          wx.hideLoading()
        } else {
          wx.hideLoading()
        }
      })
    })
  },
  getPE() {
    let that = this
    let urls = that.data.queryFrom.hospitalId ? '/hosPart/list/by/' + that.data.queryFrom.hospitalId : '/medical/all/depart'
    wx.request({
      url: app.globalData.serverUrlgdb + urls,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.hospitalDepartList.map((item, index) => {
            item.value = item.hospitalDepartId
            item.label = item.deptName
            if (index == 0) {
              item.checked = true
            }
          })
          that.setData({
            responsibleOptions: resData.data.hospitalDepartList,
            'queryFrom.hospitalDepartId': resData.data.hospitalDepartList[0].hospitalDepartId
          })
          // this.getList()
        }
      })
    })
  },
  getPT() {
    let that = this
    wx.request({
      url: app.globalData.serverUrlgdb + '/hospital/list',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          console.log(resData.data);
          resData.data.hospitalList.map(item => {
            item.value = item.hospitalId
            item.label = item.hospitalName
          })
          that.setData({
            responsibleList: resData.data.hospitalList
          })
        }
      })
    })
  },
  getPEdiy() {
    let that = this
    let urls = that.data.queryFrom.hospitalId ? '/hosPart/list/by/' + that.data.queryFrom.hospitalId : '/medical/all/depart'
    wx.request({
      url: app.globalData.serverUrlgdb + urls,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          if(resData.data.hospitalDepartList.length==1){
            that.setData({
              'queryFrom.hospitalDepartId': resData.data.hospitalDepartList[0].deptId
            })
          }else{
            that.setData({
              'queryFrom.hospitalDepartId': ''
            })
          }
          
          that.getList()
        }
      })
    })
  },
  bindChangeterritoryjd(e) {
    this.setData({
      'orderData.territory': e.detail.label,
      'orderData.province': Number(e.detail.value),
    })
    this.getgrade()
    this.getgraderate()
    this.selectComponent('#territory').hideModal()
  },
  bindChangedutyjd(e) {
    this.setData({
      'orderData.duty': e.detail.label,
      'orderData.salesPerson': e.detail.value,
    })
    this.getgrade()
    this.getgraderate()
    this.selectComponent('#duty').hideModal()
  },
  bindChangeResponsible(e) {
    this.setData({
      responsible: e.detail.label,
      'queryFrom.hospitalDepartId': e.detail.value
    })
    this.getList()
  },
  bindChangeResponsiblejd(e) {
    console.log(e.detail.value);
    this.setData({
      responsible: e.detail.label,
      'queryFrom.hospitalId': e.detail.value,
      'queryFrom.hospitalDepartId': ''
    })
    this.getList()
    // this.getPE()
  },
  synctabCountinfo(e){
    console.log(e.detail.value,'试试');
    let that = this
    that.setData({
      'queryFrom.hospitalId':e.detail.value,
    })
    that.getPEdiy()
    // that.getList()
  },
  syncCount(e) {
    console.log(e);
    wx.navigateTo({
      url: '/pagesB/statisticsquery/statisticsquery?id=' + JSON.stringify(e.detail.value)
    })
    return
    const id = e.detail.value
    console.log(e.detail);
    this.selectComponent('#' + id).show()
  },
  territoryCount(e) {
    console.log(e);
    const id = e.detail.value
    console.log(e.detail);
    this.selectComponent('#' + id).show()
  },
  synctabCount(e) {
    console.log(e.detail.value);
    this.setData({
      istype: e.detail.value,
      queryFrom: {}
    })
    this.getTyoeList()
  },

  tabNY() {
    if (this.data.nyinit == 0) {
      this.setData({
        nyinit: 1,
      })
    } else {
      this.setData({
        nyinit: 0,
      })
    }
    this.setData({
      date: this.fromaTime(new Date()),
      dateNo: this.fromaTimes(new Date())
    })
    this.getList()
    this.getListzrr()
    this.getdisList()
  },
  bindDateChange: function (e) {
    let str = e.detail.value
    if (str.indexOf('-') == -1) {
      console.log(str.indexOf('-'));
      str += '年'
    } else {
      str = str.replace(/-/g, '年') + '月'
    }
    console.log(e, 'picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      date: str,
      dateNo: e.detail.value
    })
    this.getList()
    this.getListzrr()
    this.getdisList()
  },
  fromaTime(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '年' + month + '月'
    } else {
      return year + '年'
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  fromaTimes(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '-' + (month < 10 ? ('0' + month) : month)
    } else {
      return year + ''
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      istyp: null,
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      'orderData.territory':'',
      'orderData.province':'',
      'orderData.duty':'',
      'orderData.salesPerson':''
    })
    this.getListzrr()
    this.getdisList()
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})